package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.McDetailInfoMapper;
import cc.rengu.igas.momp.common.dao.MompTMMcFundInfoMapper;
import cc.rengu.igas.momp.common.dao.PlatBizTmpMapper;
import cc.rengu.igas.momp.common.dao.impl.McDetailInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.MompTMMcFundInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.PlatBizTmpMapperImpl;
import cc.rengu.igas.momp.common.entity.MompTMMcFundInfo;
import cc.rengu.igas.momp.common.entity.TMMcCouponInfo;
import cc.rengu.igas.momp.common.entity.TMMcMoneyOffInfo;
import cc.rengu.igas.momp.common.entity.TMPlatBizTmp;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.util.McInfoDbOperation;
import cc.rengu.igas.momp.common.util.MompDateUtil;
import cc.rengu.igas.momp.common.util.MompToolUtil;
import cc.rengu.igas.momp.common.util.RbdpClient;
import cc.rengu.igas.momp.facade.request.MchntMcPublishRequest;
import cc.rengu.igas.momp.facade.response.MchntMcPublishResponse;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.jrbdp.register.realtime.client.RealTimeRegisterClient;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.base.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商户营销活动发布
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2020/6/15
 */
public class MchntMcPublishService extends RadpService {

    private String globalInstId;
    private String globalMcNo;
    private String globalTimeStamp;
    private String globalOprId;
    private String queryType;
    private String queryRole;
    private String oprType;
    private String mcStatus;
    private String globalmcType;
    private String globalTraceNo;
    private String globalTxnDate;
    /**
     * 批量任务注册参数类型 PARAM_TYPE
     */
    private static final String MOMP_RBDP_ADDRESS = "MOMP_RBDP_ADDRESS";
    /**
     * 批量任务注册地址参数键值 PARAM_KEY
     */
    private static final String MOMP_RBDP_REGISTER_ADDRESS = "MOMP_RBDP_REGISTER_ADDRESS";
    /**
     * 强制终止原因
     */
    private String oprContent;
    /**
     * 活动审批状态
     */
    private String auditStatus;
    /**
     * 状态
     */
    private String bizStatus;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {
            MchntMcPublishRequest mchntMcPublishRequest = new MchntMcPublishRequest();
            ConvertUtil.convertOutput(mchntMcPublishRequest);

            /* 检查请求报文并赋值全局变量 */
            messageValidation(mchntMcPublishRequest);

            MchntMcPublishResponse mchntMcPublishResponse = new MchntMcPublishResponse();
            mchntMcPublishResponse.setHeader(mchntMcPublishRequest.getHeader());
            BizResponse<MchntMcPublishResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(mchntMcPublishResponse);

            // 服务调用
            BizResponse<MchntMcPublishResponse> bizResponseNew = mchntMcPublish(mchntMcPublishRequest, mchntMcPublishResponse, xmlTreeUtil);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 商户发布减免类或卡券类营销活动
     *
     * @param mchntMcPublishRequest  减免类活动发布接口请求对象
     * @param mchntMcPublishResponse 减免类活动发布接口应答对象
     * @param xmlTreeUtil            内部XML树
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.MchntMcPublishResponse> 减免类活动发布接口应答对象
     * @throws BizException 异常信息
     * @author liujinan
     * @date 2020/6/15 16:59
     */
    private BizResponse<MchntMcPublishResponse> mchntMcPublish(MchntMcPublishRequest mchntMcPublishRequest, MchntMcPublishResponse mchntMcPublishResponse, XmlTreeUtil xmlTreeUtil) throws Exception {

        BizResponse<MchntMcPublishResponse> bizResponse = new BizResponse<>();

        //判断用户的查询类型 0-测试后发布活动 1-启用活动 2-暂停活动 3-强制终止活动  4-打款成功后通知（更新活动为测试中）
        //商户人员操作
        if (QueryTypeEnum.MCHNT_QYERY_TYPE.getQueryTypeCode().equals(queryType)) {
            //检查营销活动状态,并赋值活动号
            checkMcStatus();
            // 判断操作类型
            if (OprTypeEnum.PUBLISH_PASSED.getOprTypeCode().equals(oprType)) {
                rglog.info("机构<{}> 流水<{}> 营销活动编号<{}> 测试后发布流程开始", globalInstId, globalTraceNo, globalMcNo);
                // 测试后发布
                publishMc(xmlTreeUtil);

            } else if (OprTypeEnum.ENABLE.getOprTypeCode().equals(oprType) ||
                    OprTypeEnum.PAUSE.getOprTypeCode().equals(oprType) ||
                    OprTypeEnum.FORCE_DISABLE.getOprTypeCode().equals(oprType)) {
                rglog.info("机构<{}> 流水<{}> 营销活动编号<{}> 操作类型<{}> 启用(2),暂停(3),强制终止(4),打款后通知(5) 流程开始", globalInstId, globalTraceNo, globalMcNo, oprType);
                // 启用活动,暂停,或强制终止
                enableOrPauseOrforceDisableMc(xmlTreeUtil, oprType);
            } else if (OprTypeEnum.FORCE_PAYMENT_NOTIFY.getOprTypeCode().equals(oprType)) {
                rglog.info("机构<{}> 流水<{}> 营销活动编号<{}> 打款成功后通知流程开始（修改活动状态为测试中）", globalInstId, globalTraceNo, globalMcNo);
                // 打款成功后通知
                paymentSuccessNotify(xmlTreeUtil);

            } else {
                // 不支持的操作类型
                rglog.error("<{}>, OPR_TYPE=<{}>", RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc(), oprType);
                throw new BizException(RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc());
            }
        } else {
            // 非商户人员无权限操作数据
            rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", queryRole, queryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        mchntMcPublishResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        mchntMcPublishResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntMcPublishResponse);

        return bizResponse;
    }

    /**
     * 判断营销活动状态是否允许当前的操作,未抛异常说明营销活动状态正常
     *
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 22:10
     */
    private void checkMcStatus() throws Exception {

        //查询正式表数据
        TMMcMoneyOffInfo tmMcMoneyOffInfo = (TMMcMoneyOffInfo) McInfoDbOperation.getMcInfo(rglog, globalInstId, globalMcNo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
        TMMcCouponInfo tmMcCouponInfo = (TMMcCouponInfo) McInfoDbOperation.getMcInfo(rglog, globalInstId, globalMcNo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());

        if (null == tmMcMoneyOffInfo && null == tmMcCouponInfo) {
            rglog.error("未获取到机构<{}>的营销活动编号<{}>的营销活动基本信息", globalInstId, globalMcNo);
            throw new BizException(RespCodeEnum.GET_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_MC_LIST_ERROR.getRespDesc());
        }

        //查询临时表数据
        TMPlatBizTmp moneyOffTMPlatBizTmp = McInfoDbOperation.getTmpMcInfo(rglog, globalInstId, globalMcNo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
        TMPlatBizTmp couponTMPlatBizTmp = McInfoDbOperation.getTmpMcInfo(rglog, globalInstId, globalMcNo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());

        if (null == moneyOffTMPlatBizTmp && null == couponTMPlatBizTmp) {
            rglog.error("未获取到机构<{}>的营销活动编号<{}>的营销活动临时表信息", globalInstId, globalMcNo);
            throw new BizException(RespCodeEnum.GET_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_MC_LIST_ERROR.getRespDesc());
        }
        if (null != moneyOffTMPlatBizTmp) {
            bizStatus = moneyOffTMPlatBizTmp.getBizStatus();
            auditStatus = moneyOffTMPlatBizTmp.getAuditStatus();
        }
        if (null != couponTMPlatBizTmp) {
            bizStatus = couponTMPlatBizTmp.getBizStatus();
            auditStatus = couponTMPlatBizTmp.getAuditStatus();
        }

        if (null != tmMcMoneyOffInfo && null != tmMcCouponInfo) {
            rglog.error("获取到机构<{}>的营销活动编号<{}>的营销活动基本信息多条,错误", globalInstId, globalMcNo);
            throw new BizException(RespCodeEnum.GET_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_MC_LIST_ERROR.getRespDesc());
        }
        //接口请求的操作类型和减免或卡券的不一样,做个转换
        String requestOprType = oprType;
        switch (requestOprType) {
            case "0":
                oprType = OprTypeEnum.PUBLISH_PASSED.getOprTypeCode();
                break;
            case "1":
                oprType = OprTypeEnum.ENABLE.getOprTypeCode();
                break;
            case "2":
                oprType = OprTypeEnum.PAUSE.getOprTypeCode();
                break;
            case "3":
                oprType = OprTypeEnum.FORCE_DISABLE.getOprTypeCode();
                break;
            case "4":
                oprType = OprTypeEnum.FORCE_PAYMENT_NOTIFY.getOprTypeCode();
                break;
            default:
                break;
        }

        if (null != tmMcMoneyOffInfo && null != moneyOffTMPlatBizTmp) {
            //如果有活动编号上送，查询数据库存储的原操作主体（商户号），如果和当前不一致，不允许操作
            String oldOprId = tmMcMoneyOffInfo.getOprId();
            String thisOprId = globalOprId;
            if (!oldOprId.equals(thisOprId)) {
                rglog.error("活动 <{}>,原查询主体 <{}>,当前操作上送的查询主体<{}>,两者不同，不允许操作!", globalMcNo, oldOprId, thisOprId);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }

            mcStatus = tmMcMoneyOffInfo.getMcStatus();
            globalmcType = McTypeEnum.MC_TYPE_MO.getMcTypeCode();
            globalMcNo = tmMcMoneyOffInfo.getMcNo();

            if (OprTypeEnum.PUBLISH_PASSED.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.TESTING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) &&
                        !McStatusEnum.TESTING.getMcStatusCode().equals(moneyOffTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不可发布通过", globalInstId, globalMcNo, tmMcMoneyOffInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_PASS_PUBLISH_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_PASS_PUBLISH_ERROR.getRespDesc());
                }
            } else if (OprTypeEnum.ENABLE.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.PAUSED.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) &&
                        !McStatusEnum.PAUSED.getMcStatusCode().equals(moneyOffTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不可启用", globalInstId, globalMcNo, tmMcMoneyOffInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_ENABLE_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_ENABLE_ERROR.getRespDesc());
                }
            } else if (OprTypeEnum.PAUSE.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) &&
                        !McStatusEnum.PROGRESSING.getMcStatusCode().equals(moneyOffTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不可暂停", globalInstId, globalMcNo, tmMcMoneyOffInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_PAUSE_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_PAUSE_ERROR.getRespDesc());
                }
            } else if (OprTypeEnum.FORCE_DISABLE.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.TESTING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) &&
                        !McStatusEnum.TESTING.getMcStatusCode().equals(moneyOffTMPlatBizTmp.getMcStatus()) && !McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) &&
                        !McStatusEnum.PROGRESSING.getMcStatusCode().equals(moneyOffTMPlatBizTmp.getMcStatus()) && !McStatusEnum.PAUSED.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) &&
                        !McStatusEnum.PAUSED.getMcStatusCode().equals(moneyOffTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不可强制结束活动", globalInstId, globalMcNo, tmMcMoneyOffInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_FORCE_DISABLE_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_FORCE_DISABLE_ERROR.getRespDesc());
                }
            } else if (OprTypeEnum.FORCE_PAYMENT_NOTIFY.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) &&
                        !McStatusEnum.DRAFT.getMcStatusCode().equals(moneyOffTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不接受打款成功通知", globalInstId, globalMcNo, tmMcMoneyOffInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_RECEIVE_NOTIFY.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_RECEIVE_NOTIFY.getRespDesc());
                }
            } else {
                //不支持的操作类型
                rglog.error("<{}>, OPR_TYPE=<{}>", RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc(), requestOprType);
                throw new BizException(RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc());
            }
        }
        if (null != tmMcCouponInfo && null != couponTMPlatBizTmp) {
            //如果有活动编号上送，查询数据库存储的原操作主体（商户号），如果和当前不一致，不允许操作
            String oldOprId = tmMcCouponInfo.getOprId();
            String thisOprId = globalOprId;
            if (!oldOprId.equals(thisOprId)) {
                rglog.error("活动 <{}>,原查询主体 <{}>,当前操作上送的查询主体<{}>,两者不同，不允许操作!", globalMcNo, oldOprId, thisOprId);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }

            mcStatus = tmMcCouponInfo.getMcStatus();
            globalmcType = McTypeEnum.MC_TYPE_CP.getMcTypeCode();
            globalMcNo = tmMcCouponInfo.getMcNo();

            if (OprTypeEnum.PUBLISH_PASSED.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.TESTING.getMcStatusCode().equals(tmMcCouponInfo.getMcStatus()) &&
                        !McStatusEnum.TESTING.getMcStatusCode().equals(couponTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不可发布通过", globalInstId, globalMcNo, tmMcCouponInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_PASS_PUBLISH_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_PASS_PUBLISH_ERROR.getRespDesc());
                }
            } else if (OprTypeEnum.ENABLE.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.PAUSED.getMcStatusCode().equals(tmMcCouponInfo.getMcStatus()) &&
                        !McStatusEnum.PAUSED.getMcStatusCode().equals(couponTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>营销活动状态<{}>异常，不可启用", globalInstId, globalMcNo, tmMcCouponInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_ENABLE_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_ENABLE_ERROR.getRespDesc());
                }
            } else if (OprTypeEnum.PAUSE.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcCouponInfo.getMcStatus()) &&
                        !McStatusEnum.PROGRESSING.getMcStatusCode().equals(couponTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不可暂停", globalInstId, globalMcNo, tmMcCouponInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_PAUSE_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_PAUSE_ERROR.getRespDesc());
                }
            } else if (OprTypeEnum.FORCE_DISABLE.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.TESTING.getMcStatusCode().equals(tmMcCouponInfo.getMcStatus()) &&
                        !McStatusEnum.TESTING.getMcStatusCode().equals(couponTMPlatBizTmp.getMcStatus()) && !McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcCouponInfo.getMcStatus()) &&
                        !McStatusEnum.PROGRESSING.getMcStatusCode().equals(couponTMPlatBizTmp.getMcStatus()) && !McStatusEnum.PAUSED.getMcStatusCode().equals(tmMcCouponInfo.getMcStatus()) &&
                        !McStatusEnum.PAUSED.getMcStatusCode().equals(couponTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不可强制结束活动", globalInstId, globalMcNo, tmMcCouponInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_FORCE_DISABLE_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_FORCE_DISABLE_ERROR.getRespDesc());
                }
            } else if (OprTypeEnum.FORCE_PAYMENT_NOTIFY.getOprTypeCode().equals(oprType)) {
                if (!McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcCouponInfo.getMcStatus()) &&
                        !McStatusEnum.DRAFT.getMcStatusCode().equals(couponTMPlatBizTmp.getMcStatus())) {
                    rglog.error("机构<{}>的营销活动编号<{}>的营销活动状态<{}>异常，不可接受打款通知成功！", globalInstId, globalMcNo, tmMcCouponInfo.getMcStatus());
                    throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_RECEIVE_NOTIFY.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_RECEIVE_NOTIFY.getRespDesc());
                }
            } else {
                //不支持的操作类型
                rglog.error("<{}>, OPR_TYPE=<{}>", RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc(), requestOprType);
                throw new BizException(RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc());
            }
        }
    }

    /**
     * 测试后发布流程
     *
     * @param xmlTreeUtil 内部XML树
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 22:50
     */
    private void publishMc(XmlTreeUtil xmlTreeUtil) throws Exception {

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        int returnCode;

        if (McTypeEnum.MC_TYPE_MO.getMcTypeCode().equals(globalmcType)) {
            //更新减免类营销活动正式表活动状态和审批状态
            TMMcMoneyOffInfo tmMcMoneyOffInfo = new TMMcMoneyOffInfo();
            tmMcMoneyOffInfo.setInstId(globalInstId);
            tmMcMoneyOffInfo.setMcNo(globalMcNo);
            tmMcMoneyOffInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
            tmMcMoneyOffInfo.setAuditStatus(AuditStatusEnum.PUBLISH_AUDIT_PASSED.getAuditStatusCode());
            tmMcMoneyOffInfo.setLastOprId(globalOprId);
            tmMcMoneyOffInfo.setUpdateTime(globalTimeStamp);
            returnCode = updateMoneyOffMcStatusForPublishing(tmMcMoneyOffInfo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
            if (Database.DBS_SUCCESS != returnCode) {
                rglog.error("营销活动发布时更新减免类营销活动信息表失败！RETURN_CODE=<{}>", returnCode);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        } else {
            //更新卡券类营销活动正式表活动状态和审批状态
            TMMcCouponInfo tmMcCouponInfo = new TMMcCouponInfo();
            tmMcCouponInfo.setInstId(globalInstId);
            tmMcCouponInfo.setMcNo(globalMcNo);
            tmMcCouponInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
            tmMcCouponInfo.setAuditStatus(AuditStatusEnum.PUBLISH_AUDIT_PASSED.getAuditStatusCode());
            tmMcCouponInfo.setLastOprId(globalOprId);
            tmMcCouponInfo.setUpdateTime(globalTimeStamp);
            returnCode = updateCouponMcStatusForPublishing(tmMcCouponInfo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());
            if (Database.DBS_SUCCESS != returnCode) {
                rglog.error("营销活动发布时更新卡券类营销活动信息表失败！RETURN_CODE=<{}>", returnCode);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        /* 更新业务数据临时表状态 */
        TMPlatBizTmp tmPlatBizTmp1 = new TMPlatBizTmp();
        tmPlatBizTmp1.setInstId(globalInstId);
        tmPlatBizTmp1.setMcNo(globalMcNo);
        tmPlatBizTmp1.setBizOpr(BizOprEnum.PUBLISH.getBizOprCode());
        tmPlatBizTmp1.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
        tmPlatBizTmp1.setAuditStatus(AuditStatusEnum.PUBLISH_AUDIT_PASSED.getAuditStatusCode());
        returnCode = updateSingleMcInfoForPublishing(tmPlatBizTmp1);
        if (Database.DBS_SUCCESS != returnCode) {
            rglog.error("发布成功更新业务数据临时表失败！RETURN_CODE=<{}>", returnCode);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        dbsUtil.dbsEndTransaction(true);

    }


    /**
     * 发布成功更新临时表营销活动数据的状态、营销活动状态和审批状态
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 22:33
     */
    private int updateSingleMcInfoForPublishing(TMPlatBizTmp tmPlatBizTmp) throws Exception {

        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.updateSingleMcInfoForPublishing(tmPlatBizTmp);
    }

    /**
     * 发布成功更新减免类正式表营销活动的营销活动状态和审批状态
     *
     * @param tmMcMoneyOffInfo 减免类营销活动信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 22:45
     */////
    private int updateMoneyOffMcStatusForPublishing(TMMcMoneyOffInfo tmMcMoneyOffInfo, String tableName) throws Exception {

        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMoneyOffInfo, tableName);
    }

    /**
     * 发布成功更新卡券类正式表营销活动的营销活动状态和审批状态
     *
     * @param tmMcCouponInfo 卡券类营销活动信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 22:45
     */
    private int updateCouponMcStatusForPublishing(TMMcCouponInfo tmMcCouponInfo, String tableName) throws Exception {

        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.updateMcStatusForPublishing(tmMcCouponInfo, tableName);
    }

    /**
     * 启用已暂停营销活动更新正式表数据
     *
     * @param tmMcMoneyOffInfo 减免类营销活动信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 01:43
     */
    private int updatePausedMoneyOffMcMcStatusForEnabling(TMMcMoneyOffInfo tmMcMoneyOffInfo, String tableName) throws Exception {

        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.updatePausedMcMcStatusForEnabling(tmMcMoneyOffInfo, tableName);
    }

    /**
     * 启用已暂停营销活动更新正式表数据
     *
     * @param tmMcCouponInfo 卡券类营销活动信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 01:43
     */
    private int updatePausedCouponMcMcStatusForEnabling(TMMcCouponInfo tmMcCouponInfo, String tableName) throws Exception {

        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.updatePausedMcMcStatusForEnabling(tmMcCouponInfo, tableName);
    }

    /**
     * 启用已暂停营销活动更新临时表数据
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 01:37
     */
    private int updatePausedMcForEnabling(TMPlatBizTmp tmPlatBizTmp) throws Exception {

        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.updatePausedMcForEnabling(tmPlatBizTmp);
    }

    /**
     * 启用,暂停,强制终止营销活动
     *
     * @param xmlTreeUtil 内部XML树
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/15 01:28
     */
    private void enableOrPauseOrforceDisableMc(XmlTreeUtil xmlTreeUtil, String operType) throws Exception {

        int returnCode = 1;
        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        //更新临时表数据状态
        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
        tmPlatBizTmp.setInstId(globalInstId);
        tmPlatBizTmp.setMcNo(globalMcNo);
        tmPlatBizTmp.setBizStatus(bizStatus);
        tmPlatBizTmp.setAuditStatus(auditStatus);
        tmPlatBizTmp.setLastOprId(globalOprId);
        tmPlatBizTmp.setLastUpdateTime(globalTimeStamp);

        if (McTypeEnum.MC_TYPE_MO.getMcTypeCode().equals(globalmcType)) {
            //更新减免类营销活动正式表活动状态和审批状态
            TMMcMoneyOffInfo tmMcMoneyOffInfo = new TMMcMoneyOffInfo();
            tmMcMoneyOffInfo.setInstId(globalInstId);
            tmMcMoneyOffInfo.setMcNo(globalMcNo);
            tmMcMoneyOffInfo.setLastOprId(globalOprId);
            tmMcMoneyOffInfo.setUpdateTime(globalTimeStamp);

            if (OprTypeEnum.ENABLE.getOprTypeCode().equals(operType)) {
                tmMcMoneyOffInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
                tmPlatBizTmp.setBizOpr(BizOprEnum.ENABLE.getBizOprCode());
                tmPlatBizTmp.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
            }
            if (OprTypeEnum.PAUSE.getOprTypeCode().equals(operType)) {
                tmMcMoneyOffInfo.setMcStatus(McStatusEnum.PAUSED.getMcStatusCode());
                tmPlatBizTmp.setBizOpr(BizOprEnum.SUSPEND.getBizOprCode());
                tmPlatBizTmp.setMcStatus(McStatusEnum.PAUSED.getMcStatusCode());
            }
            if (OprTypeEnum.FORCE_DISABLE.getOprTypeCode().equals(operType)) {
                tmMcMoneyOffInfo.setMcStatus(McStatusEnum.FORCE_ENDED.getMcStatusCode());
                //活动截止日期 变更为当前强停时间
                tmMcMoneyOffInfo.setMcEndDate(DateUtil.getCurrentDate());
                //强制终止原因
                if (!StringUtil.isNullorEmpty(oprContent)) {
                    tmMcMoneyOffInfo.setNfrcTmnlRsn(oprContent);
                }
                tmPlatBizTmp.setBizOpr(BizOprEnum.SUSPEND.getBizOprCode());
                tmPlatBizTmp.setMcStatus(McStatusEnum.FORCE_ENDED.getMcStatusCode());
            }
            returnCode = updatePausedMoneyOffMcMcStatusForEnabling(tmMcMoneyOffInfo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
            if (Database.DBS_SUCCESS != returnCode) {
                rglog.error("减免类营销活动信息正式表更新失败！RETURN_CODE=<{}>", returnCode);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        } else {
            //更新卡券类营销活动正式表活动状态和审批状态
            TMMcCouponInfo tmMcCouponInfo = new TMMcCouponInfo();
            tmMcCouponInfo.setInstId(globalInstId);
            tmMcCouponInfo.setMcNo(globalMcNo);
            tmMcCouponInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
            tmMcCouponInfo.setLastOprId(globalOprId);
            tmMcCouponInfo.setUpdateTime(globalTimeStamp);

            if (OprTypeEnum.ENABLE.getOprTypeCode().equals(operType)) {
                tmMcCouponInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
                tmPlatBizTmp.setBizOpr(BizOprEnum.ENABLE.getBizOprCode());
                tmPlatBizTmp.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
            }
            if (OprTypeEnum.PAUSE.getOprTypeCode().equals(operType)) {
                tmMcCouponInfo.setMcStatus(McStatusEnum.PAUSED.getMcStatusCode());
                tmPlatBizTmp.setBizOpr(BizOprEnum.SUSPEND.getBizOprCode());
                tmPlatBizTmp.setMcStatus(McStatusEnum.PAUSED.getMcStatusCode());
            }
            if (OprTypeEnum.FORCE_DISABLE.getOprTypeCode().equals(operType)) {
                tmMcCouponInfo.setMcStatus(McStatusEnum.FORCE_ENDED.getMcStatusCode());
                //活动截止日期 变更为当前强停时间
                tmMcCouponInfo.setMcEndDate(DateUtil.getCurrentDate());
                //强制终止原因
                if (!StringUtil.isNullorEmpty(oprContent)) {
                    tmMcCouponInfo.setNfrcTmnlRsn(oprContent);
                }
                tmPlatBizTmp.setBizOpr(BizOprEnum.SUSPEND.getBizOprCode());
                tmPlatBizTmp.setMcStatus(McStatusEnum.FORCE_ENDED.getMcStatusCode());
            }

            returnCode = updatePausedCouponMcMcStatusForEnabling(tmMcCouponInfo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());
            if (Database.DBS_SUCCESS != returnCode) {
                rglog.error("卡券类营销活动信息正式表更新失败！RETURN_CODE=<{}>", returnCode);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }

        returnCode = updatePausedMcForEnabling(tmPlatBizTmp);
        if (Database.DBS_SUCCESS != returnCode) {
            rglog.error("营销活动信息临时表更新失败！RETURN_CODE=<{}>", returnCode);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 全局变量赋值
     *
     * @param mchntMcPublishRequest 接口请求对象
     * @throws BizException 异常信息
     * @author liujinan
     * @date 2020/9/13 16:37
     */
    private void messageValidation(MchntMcPublishRequest mchntMcPublishRequest) throws Exception {

        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
        globalTxnDate = mchntMcPublishRequest.getHeader().getTransDate();
        if (StringUtil.isNullorEmpty(mchntMcPublishRequest.getHeader().getInstId())) {
            rglog.error("机构编码必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = mchntMcPublishRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(mchntMcPublishRequest.getQueryId())) {
            rglog.error("查询主体必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalOprId = mchntMcPublishRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(mchntMcPublishRequest.getQueryType())) {
            rglog.error("查询类型必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            queryType = mchntMcPublishRequest.getQueryType();
        }

        if (StringUtil.isNullorEmpty(mchntMcPublishRequest.getQueryRole())) {
            rglog.error("查询主体角色必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            queryRole = mchntMcPublishRequest.getQueryRole();
        }

        if (StringUtil.isNullorEmpty(mchntMcPublishRequest.getOprType())) {
            rglog.error("操作类型必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            oprType = mchntMcPublishRequest.getOprType();
        }

        if (StringUtil.isNullorEmpty(mchntMcPublishRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalTraceNo = mchntMcPublishRequest.getHeader().getTraceNo();
        }

        if (StringUtil.isNullorEmpty(mchntMcPublishRequest.getMcNo())) {
            rglog.error("营销活动编号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalMcNo = mchntMcPublishRequest.getMcNo();
            if (globalMcNo != null) {
                //查询正式表数据
                TMMcMoneyOffInfo tmMcMoneyOffInfo = null;
                TMMcCouponInfo tmMcCouponInfo = null;
                tmMcMoneyOffInfo = (TMMcMoneyOffInfo) McInfoDbOperation.getMcInfo(rglog, globalInstId, globalMcNo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
                tmMcCouponInfo = (TMMcCouponInfo) McInfoDbOperation.getMcInfo(rglog, globalInstId, globalMcNo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());

                if (null == tmMcMoneyOffInfo && null == tmMcCouponInfo) {
                    rglog.error("未获取到机构<{}>的营销活动编号<{}>的营销活动基本信息", globalInstId, globalMcNo);
                    throw new BizException(RespCodeEnum.GET_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_MC_LIST_ERROR.getRespDesc());
                }
                /*减免类*/
                if (null != tmMcMoneyOffInfo && null != tmMcMoneyOffInfo.getOprId()) {
                    if (!globalOprId.equals(tmMcMoneyOffInfo.getOprId())) {
                        rglog.error("当前【角色】：<{}>的查询【用户】:<{}>无权限操作数据!", queryRole, globalOprId);
                        throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
                    }
                }
                /*卡卷类*/
                if (null != tmMcCouponInfo && null != tmMcCouponInfo.getOprId()) {
                    if (!globalOprId.equals(tmMcCouponInfo.getOprId())) {
                        rglog.error("当前【角色】：<{}>的查询【用户】:<{}>无权限操作数据!", queryRole, globalOprId);
                        throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
                    }
                }
            }
        }
        oprContent = mchntMcPublishRequest.getOprContent();
    }

    /**
     * 打款成功通知
     *
     * @param xmlTreeUtil 内部XML树
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/9/18 22:50
     */
    private void paymentSuccessNotify(XmlTreeUtil xmlTreeUtil) throws Exception {

        MompTMMcFundInfoMapper tmMcFundInfoMapper = new MompTMMcFundInfoMapperImpl();
        List<MompTMMcFundInfo> MompTMMcFundInfoList = tmMcFundInfoMapper.selectMompTMMcFundInfoByInstIdAndMcNoAndDescByCreateTime(globalInstId, globalMcNo, globalOprId, queryType);
        if (MompTMMcFundInfoList == null || MompTMMcFundInfoList.size() == 0) {
            //没有打款记录，异常
            rglog.info("机构<{}> 商户<{}> 营销活动<{}> 没有打款记录,不接受打款成功通知，异常！", globalInstId, globalOprId, globalMcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_FUND_INFO_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_FUND_INFO_ERROR.getRespDesc());
        } else {
            //存在打款记录,判断最新打款记录的打款状态
            MompTMMcFundInfo lastMompTMMcFundInfo = MompTMMcFundInfoList.get(CommonConstant.ZERO);
            if (CommonConstant.ZERO_COMMON_CONSTANT.equals(lastMompTMMcFundInfo.getFundStatus())) {
                //已成功打款
                rglog.info("机构<{}> 商户<{}> 营销活动<{}> 已打款成功,打款流水号<{}> 流程继续！", globalInstId, globalOprId, globalMcNo, lastMompTMMcFundInfo.getRecordIndex());
            } else {
                //没有打款记录，异常
                rglog.info("机构<{}> 商户<{}> 营销活动<{}> 打款失败,打款流水号<{}> 不接受打款成功通知，异常！", globalInstId, globalOprId, globalMcNo, lastMompTMMcFundInfo.getRecordIndex());
                throw new BizException(RespCodeEnum.WRONG_MC_FUND_INFO_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_FUND_INFO_ERROR.getRespDesc());
            }
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        int returnCode;

        if (McTypeEnum.MC_TYPE_MO.getMcTypeCode().equals(globalmcType)) {
            //更新减免类营销活动正式表活动状态和审批状态
            TMMcMoneyOffInfo tmMcMoneyOffInfo = new TMMcMoneyOffInfo();
            tmMcMoneyOffInfo.setInstId(globalInstId);
            tmMcMoneyOffInfo.setMcNo(globalMcNo);
            tmMcMoneyOffInfo.setMcStatus(McStatusEnum.TESTING.getMcStatusCode());
            tmMcMoneyOffInfo.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PASSED.getAuditStatusCode());
            tmMcMoneyOffInfo.setLastOprId(globalOprId);
            tmMcMoneyOffInfo.setUpdateTime(globalTimeStamp);
            returnCode = updateMoneyOffMcStatusForPublishing(tmMcMoneyOffInfo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
            if (Database.DBS_SUCCESS != returnCode) {
                rglog.error("营销活动打款成功处理通知时更新减免类营销活动信息表失败！RETURN_CODE=<{}>", returnCode);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespDesc());
            }
        } else {
            //更新卡券类营销活动正式表活动状态和审批状态
            TMMcCouponInfo tmMcCouponInfo = new TMMcCouponInfo();
            tmMcCouponInfo.setInstId(globalInstId);
            tmMcCouponInfo.setMcNo(globalMcNo);
            tmMcCouponInfo.setMcStatus(McStatusEnum.TESTING.getMcStatusCode());
            tmMcCouponInfo.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PASSED.getAuditStatusCode());
            tmMcCouponInfo.setLastOprId(globalOprId);
            tmMcCouponInfo.setUpdateTime(globalTimeStamp);
            returnCode = updateCouponMcStatusForPublishing(tmMcCouponInfo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());
            if (Database.DBS_SUCCESS != returnCode) {
                rglog.error("营销活动打款成功处理通知时更新卡券类营销活动信息表失败！RETURN_CODE=<{}>", returnCode);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespDesc());
            }
        }
        /* 更新业务数据临时表状态 */
        TMPlatBizTmp tmPlatBizTmp1 = new TMPlatBizTmp();
        tmPlatBizTmp1.setInstId(globalInstId);
        tmPlatBizTmp1.setMcNo(globalMcNo);
        tmPlatBizTmp1.setBizOpr(BizOprEnum.PUBLISH.getBizOprCode());
        tmPlatBizTmp1.setMcStatus(McStatusEnum.TESTING.getMcStatusCode());
        tmPlatBizTmp1.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PASSED.getAuditStatusCode());
        returnCode = updateSingleMcInfoForPublishing(tmPlatBizTmp1);
        if (Database.DBS_SUCCESS != returnCode) {
            rglog.error("营销活动打款成功处理通知时更新业务数据临时表失败！RETURN_CODE=<{}>", returnCode);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.UPDATE_TMP_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_TMP_MC_STATUS_FAILED_ERROR.getRespDesc());
        }
        if (McTypeEnum.MC_TYPE_CP.getMcTypeCode().equals(globalmcType)) {
            rglog.info("注册卡券生成定时任务");
            timingTask(CommonConstant.CP_CREATE_TIME_TASK_NAME, globalTxnDate, "", globalInstId, globalMcNo, dbsUtil);
        }
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 注册定时任务
     *
     * @param taskName   任务名称
     * @param settleDate 清算日期 不上送 默认当前日期
     * @param regTime    执行日期 不上送 立即执行
     * @param taskBatch  批次号后五位清算日期内唯一
     * @param taskParam  营销活动编号
     * @throws Exception
     */
    private void timingTask(String taskName, String settleDate, String regTime, String taskBatch, String taskParam, DbsUtil dbsUtil) throws Exception {
        rglog.info("任务名称<{}> 清算日期<{}> 执行时间<{}> 批次号<{}> 营销活动编号<{}>", taskName, settleDate, regTime, taskBatch, taskParam);
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID, MOMP_RBDP_ADDRESS, MOMP_RBDP_REGISTER_ADDRESS);
        if (null == sysParam) {
            rglog.error("获取批量任务注册地址失败！");
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.FAILED_TO_GET_RBDP_REGISTER_ADDRESS.getRespCode(), RespCodeEnum.FAILED_TO_GET_RBDP_REGISTER_ADDRESS.getRespDesc());
        }
        //批量注册地址
        String rbdpserver = sysParam.getParamValue();
        RealTimeRegisterClient a = RbdpClient.getIntance1(rglog, rbdpserver);

        int ret = a.registerTask(taskName, settleDate, settleDate.concat(DateUtil.getCurrentTime()), taskBatch.concat(MompToolUtil.getAppoint(MompDateUtil.getTime(), 0, 5)), taskParam);
        rglog.info("卡券生成实时任务注册返回：ret {}", ret);
        String result = "";
        if (0 != ret) {
            result = "注册失败: " + RbdpClient.getIntance1(rglog, rbdpserver).getError();
            rglog.error("实时任务注册返回：ret {}", result);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.FAILED_TO_TIME_TASK.getRespCode(), RespCodeEnum.FAILED_TO_TIME_TASK.getRespDesc());
        } else {
            result = "注册成功.";
            rglog.info("实时任务注册返回：ret {}", result);
        }
    }
}
